package com.rs.game.player.content.warriorsguild;

import com.rs.game.Animation;
import com.rs.game.ForceTalk;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.item.Item;
import com.rs.game.npc.wguild.WGuildNPC;
import com.rs.game.player.Player;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;

public final class AnimatorGame {

	public static void useAnimator(final Player player, Item item, final WorldObject object) {
		final WarriorSet set = (WarriorSet) player.getTemporaryAttributes().get("warriors_guild_armour");
		if (set == null)
			return;
		if (!player.getInventory().containsItem(set.getArmour()[0].getId(), 1)
				|| !player.getInventory().containsItem(set.getArmour()[1].getId(), 1)
				|| !player.getInventory().containsItem(set.getArmour()[2].getId(), 1)) {
			player.sendMessage("You do not have all of the valid " + set.name().toLowerCase() + " armour pieces.");
			return;
		}
		for(Item armourItem : set.getArmour()) {
			if (armourItem == null) continue;
			player.getInventory().deleteItem(armourItem);
		}
		final WorldTile toTile = new WorldTile(player.getX(), player.getY() + 4, 0);
		player.addWalkSteps(toTile.getX(), toTile.getY());
		player.getDialogueManager().startDialogue("SimpleMessage", "You use the armour on the animator where it slowly disappears...");
		player.setNextAnimation(new Animation(827));
		WorldTasksManager.schedule(new WorldTask() {

			@Override
			public void run() {
				WGuildNPC armourNPC = new WGuildNPC(4278 + set.ordinal(), new WorldTile(object.getX(), object.getY(), 0));
				player.getHintIconsManager().addHintIcon(armourNPC, 1, -1, false);
				armourNPC.setNextAnimation(new Animation(4166));
				armourNPC.setNextForceTalk(new ForceTalk("I'm ALIVE!"));
				armourNPC.getCombat().setTarget(player);
				armourNPC.getTemporaryAttributes().put("target_name", player.getUsername());
			}

		}, 4);
	}

	public static void enterCyclopsRoom(final Player player) {
		if (player.getPlayerData().getGamePoints()[0] < 200) {
			player.getDialogueManager().startDialogue("SimpleMessage", "You need more than 200 tokens to enter the Warriors Guild.");
			return;
		}
		player.setNextWorldTile(new WorldTile(2847, player.getY(), player.getPlane()));
		player.getTemporaryAttributes().put("in_warriors_guild", true);
		WorldTasksManager.schedule(new WorldTask() {

			@Override
			public void run() {
				if (player.getTemporaryAttributes().get("in_warriors_guild") != null)  {
					if (player.getPlayerData().getGamePoints()[0] - 10 < 0) {
						leaveCyclopsRoom(player);
						player.getPlayerData().getGamePoints()[0] = 0;
						stop();
						return;
					}
					player.getPlayerData().getGamePoints()[0] -= 10;
					player.getInterfaceManager().sendOverlay(1057, player.getDisplayMode() > 1);
					player.getPackets().sendConfig(2030, player.getPlayerData().getGamePoints()[0]);
				} else {
					leaveCyclopsRoom(player);
					stop();
				}
			}

		}, 1, 50);
	}

	public static void leaveCyclopsRoom(final Player player) {
		player.setNextWorldTile(new WorldTile(2843, 3534, 2));
	}

}